package class_wuziqi;

import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Composite;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.font.FontRenderContext;
import java.awt.geom.Rectangle2D;
import java.io.IOException;

import javax.swing.ImageIcon;
import javax.swing.JPanel;

public class ChessBoard extends JPanel{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private Image blackChess;
	private Image whiteChess;
	private int chessW,chessH,leftFra,topFra;
	private ChessArray ca;
	private static ChessBoard chessBoard;
	private Chess chess;
	private String message="";
	public static ChessBoard getInstance(){
		return chessBoard;
	}
	public ChessBoard() {
		chessBoard=this;
		ca=ChessArray.getInstance();
		setOpaque(false);
		blackChess=new ImageIcon(getClass().getResource("/pic/blackChessman.png")).getImage();
		whiteChess=new ImageIcon(getClass().getResource("/pic/whiteChessman.png")).getImage();
		this.addMouseListener(new MouseAdapter() { 
			@Override
			public void mouseClicked(MouseEvent e){
				if(MainPro.getInstance().isStart==false||MainPro.getInstance().isMy==false) return;
				int i=(e.getX()-leftFra+chessW/2)/chessW;
				int j=(e.getY()-topFra+chessH/2)/chessH;
				if(ca.getChess(i, j)==null){
				chess=new Chess(i, j, MainPro.getInstance().chessColor);
				ca.setChess(chess);
				MainPro.getInstance().isMy=false;
				try {
					MainPro.getInstance().sendMessage(Login.getInstance().getSocket(), chess);
				} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
				repaint();
				}
				int count1=1,count2=1,count3=1,count4=1;
				int ix=chess.getI(),jx=chess.getJ();
				Color cl=chess.getColor();
				for(int a=jx-1;a>=0&&ca.getChess(ix, a)!=null&&ca.getChess(ix, a).getColor()==cl;a--){
					count1++;
				}
				for(int a=jx+1;a<15&&ca.getChess(ix, a)!=null&&ca.getChess(ix, a).getColor()==cl;a++){
					count1++;
				}
				if(count1>=5){
					try {	
						MainPro.getInstance().isOver=true;					
						MainPro.getInstance().sendMessage(Login.getInstance().getSocket(), MainPro.isWin);
					} catch (IOException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
				}	
				for(int a=ix-1;a>=0&&ca.getChess(a, jx)!=null&&ca.getChess(a, jx).getColor()==cl;a--){
					count2++;
				}
				for(int a=ix+1;a<15&&ca.getChess(a, jx)!=null&&ca.getChess(a, jx).getColor()==cl;a++){
					count2++;
				}
				if(count2>=5){
					try {
						MainPro.getInstance().isOver=true;
						MainPro.getInstance().sendMessage(Login.getInstance().getSocket(), MainPro.isWin);
					} catch (IOException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
				}	
				for(int a=ix-1,b=jx-1;a>=0&&b>=0&&ca.getChess(a, b)!=null&&ca.getChess(a, b).getColor()==cl;a--,b--){
					count3++;
				}
				for(int a=ix+1,b=jx+1;a<15&&b<15&&ca.getChess(a, b)!=null&&ca.getChess(a, b).getColor()==cl;a++,b++){
					count3++;
				}
				if(count3>=5){
					try {
						MainPro.getInstance().isOver=true;
						MainPro.getInstance().sendMessage(Login.getInstance().getSocket(), MainPro.isWin);		
					} catch (IOException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
				}	
				for(int a=ix+1,b=jx-1;a<15&&b>=0&&ca.getChess(a, b)!=null&&ca.getChess(a, b).getColor()==cl;a++,b--){
					count4++;
				}
				for(int a=ix-1,b=jx+1;a>=0&&b<15&&ca.getChess(a, b)!=null&&ca.getChess(a, b).getColor()==cl;a--,b++){
					count4++;
				}
				if(count4>=5){
					try {
						MainPro.getInstance().isOver=true;
						MainPro.getInstance().sendMessage(Login.getInstance().getSocket(), MainPro.isWin);
					} catch (IOException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
				}
			}
		});
	}
	
	@Override
	public void paint(Graphics g2) {
		Graphics2D g1=(Graphics2D)g2;
		super.paint(g1);
		drawPanel(g1); 
		for(int i=0;i<15;i++){
			for(int j=0;j<15;j++){
				if (ca.getChess(i, j)!=null) {
					if (ca.getChess(i, j).getColor().getRGB() == Color.white.getRGB()) {
						g1.drawImage(whiteChess, (leftFra + chessW * i)- chessW / 2, 
								(topFra + chessH * j) - chessH/ 2, chessW, chessH, this);
					} else if (ca.getChess(i, j).getColor().getRGB() == Color.black.getRGB()) {
						g1.drawImage(blackChess, (leftFra + chessW * i)- chessW / 2, 
								(topFra + chessH * j) - chessH/ 2, chessW, chessH, this);
					}
				}
			}
		}
		if(MainPro.getInstance().Win==true){
//			setMessage("您输了");
			drawString(g1, "您输了", new Color(0x636363), 50, 0.8f);
			Left_Panel.getInstance().setOver();
		}
		else if(MainPro.getInstance().isOver&&!MainPro.getInstance().Win){
//			setMessage("您赢了");
			drawString(g1, "您赢了", new Color(0xEE2C2C), 50, 0.8f);
			Left_Panel.getInstance().setOver();
		}
	}/*
	public void Message(String message){
		this.message=message;
	}
	public String getMessage(){
		return message;
	}
	public void setMessage(String message){
		this.message=message;
	}*/
	public void drawString(Graphics2D g,String message,Color color,int size,float alpha){
		Composite composite=g.getComposite();
		Font font=new Font("微软雅黑", Font.BOLD, size);
		g.setFont(font);
		g.setColor(color);
		g.setComposite(AlphaComposite.SrcOver.derive(alpha));
		FontRenderContext frc=g.getFontRenderContext();
		Rectangle2D r2=font.getStringBounds(message, frc);
		g.drawString(message, (int)(getWidth()-r2.getWidth())/2, (int)(getHeight()-r2.getHeight())/2);
		g.setComposite(composite);
	}
	
	private void drawPanel(Graphics2D g1) {
		Composite composite = g1.getComposite();//备份合成规则，可以使用更多功能
		Color color=g1.getColor();
		g1.setComposite(AlphaComposite.SrcOver.derive(0.9f));//设置透明合成
		g1.setColor(new Color(0xFAA755));
		g1.fill3DRect(0, 0, getWidth(), getHeight(), true);
		g1.setComposite(composite);
		g1.setColor(color);
		int width=getWidth();int height=getHeight();
		chessW=width/15;chessH=height/15;//设置棋子宽和高，棋子宽高等于棋格宽高
		leftFra=chessW/2+width%15/2;//设置棋盘左边界
		topFra=chessH/2+height%15/2;//设置棋盘上边界
		for(int i=0;i<15;i++){
			g1.drawLine(leftFra, topFra+chessH*i, leftFra+chessW*14, topFra+chessH*i);
			g1.drawLine(leftFra+chessW*i, topFra, leftFra+chessW*i, topFra+chessH*14);
		}	
	}	
}
